//
//  DownloadCell.m
//  first app
//
//  Created by 林 小明 on 14-7-7.
//
//

#import "DownloadCell.h"
#import "AsyncDownload.h"
#import "PlayManager.h"

@implementation DownloadCell

@synthesize downloadfilename;
@synthesize downloadprogessinfo;
@synthesize downloadprogress;
@synthesize downloadbutton;
@synthesize playbutton;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDownloadURL:(NSString*)url
{
    downloadurl = url;
}

- (void)startDownloadListen
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDownloadProgressInfo:) name:@"Notification_DownloadProgress" object:nil];
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPlaystatus:) name:@"Notification_Playstatus" object:nil];
}

- (void) getDownloadProgressInfo: (NSNotification*) aNotification
{
    NSMutableDictionary *downloadInfo = [aNotification object];
    NSString* downlaod_url = [downloadInfo objectForKey:@"downloadurl"];
    int downlaod_status = [[downloadInfo objectForKey:@"downloadstatus"] intValue];
    unsigned long long total_size = [[downloadInfo objectForKey:@"totalsize"] intValue];
    unsigned long long receive_size = [[downloadInfo objectForKey:@"fileoffset"] intValue];
    
    if( ![downlaod_url isEqualToString:downloadurl] )
    {
        return;
    }
    
    if( downlaod_status == kDownloadStatusFinish )
    {
        //[downloadbutton setTitle:@"Download" forState:UIControlStateNormal];
        [downloadbutton setHidden:YES];
        [playbutton setTitle:@"Play" forState:UIControlStateNormal];
        [playbutton setHidden:NO];
    }
    else
    {
        NSLog( @"get download progress total_size:%llu, receive_size:%llu, [%f]", total_size, receive_size, (double)receive_size/(double)total_size );
        
        self.downloadprogress.progress = (double)receive_size/(double)total_size;
        [downloadprogessinfo setText:[[NSString alloc] initWithFormat:@"%0.2fM/%0.2fM", (double)receive_size/(double)(1024*1024), (double)total_size/(double)(1024*1024)]];
        [downloadbutton setTitle:@"Stop" forState:UIControlStateNormal];
    }
}

- (void) showPlaystatus: (NSNotification*) aNotification
{
    NSMutableDictionary *playStatusInfo = [aNotification object];
    
    NSString* url = [playStatusInfo objectForKey:@"url"];
    int play_status = [[playStatusInfo objectForKey:@"status"] intValue];
    
    if( ![url isEqualToString:downloadurl] )
    {
        return;
    }
    
    if( [playbutton isHidden] )
    {
        return;
    }
    
    if( play_status == PlayerStatusPlay || play_status == PlayerStatusResume )
    {
        [playbutton setTitle:@"Stop" forState:UIControlStateNormal];
    }
    else if( play_status == PlayerStatusStop )
    {
        [playbutton setTitle:@"Play" forState:UIControlStateNormal];
    }
}
@end
